package cn.itcast.controller;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class ParamController {
    String paypass="liujiawan159";
    String payUrl="https://api.tianlin.art/api/order/pay?pay_type=1&jypassword="+paypass+"&order_id=";
    String token="9073324728747428";
    String createOrderUrl="https://api.tianlin.art/api/order/memberApply";
    String ipUrl="http://ip.donepx.com";
    private static List<IpBean> list=new ArrayList<>();
    @RequestMapping("/")
    public String hello(@RequestParam(value = "num",defaultValue = "1000") Integer num) {
        if(CollectionUtils.isEmpty(list)){
            try{
                String result = HttpRequest.get(ipUrl).execute().body();
                JSONArray json = JSONUtil.parseArray(result);
                for(int i=0;i<json.size();i++) {
                    JSONObject jsonObject=json.getJSONObject(i);
                    IpBean ipBean = new IpBean();
                    ipBean.setIpAddress(jsonObject.get("ip").toString());
                    ipBean.setPort(Integer.parseInt(jsonObject.get("port").toString()));
                    list.add(ipBean);
                }
            }catch (Exception e){
                IpBean ipBean=new IpBean();
                list.add(ipBean);
            }
        }
        for(int i=0;i<num;i++){
            try {
                Thread.sleep(500);
                for(int j=0;j<list.size();j++){
                    IpBean ipBean=list.get(j);
                    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ipBean.getIpAddress(), ipBean.getPort()));
                    ThreadUtil.execAsync(() -> {
                        test1(proxy);
                    });
                }
            } catch (Exception e) {
                System.out.println("#####异常####" + e.getMessage());
            }
        }
        return null;
    }
    @RequestMapping("/hello")
    public String hello1(@RequestParam(value = "num",defaultValue = "1000") Integer num) {
        Long currentTime=new Date().getTime();
        String temp="buy_passwd=891223&domain=https://m.yocho.art/&goods_num=1&id=8&key=MINGYANGB7D0C9D279892857DE5490AB856772FE&ordertype=0&pathinfo=v1/createOrder&paytype=balance&special_id=8&timestamp="+currentTime;
        String sign=SecureUtil.md5(temp);
        return null;
    }
    public void test1(Proxy proxy){
        JSONObject requestJson = new JSONObject();
        requestJson.append("id", "178");
        String result =null;
        try{
            String requestParam = requestJson.toString();
            result = HttpRequest.post(createOrderUrl).body(requestParam)
                    .setProxy(proxy)
                    .header("token",token).execute().body();
            //result="{\"code\":1,\"data\":{\"order_id\":\"123\"}}";
            System.out.println("#####创建订单返回参数####"+result+"#######"+System.currentTimeMillis());
            if(StrUtil.isNotBlank(result)){
                JSONObject str = JSONUtil.parseObj(result);
                String code=str.get("code").toString();
                if(code.equals("1")){
                    System.out.println("#####创建订单成功####"+result+"#######"+System.currentTimeMillis());
                    String data=str.get("data").toString();
                    JSONObject orderIdJson=JSONUtil.parseObj(data);
                    String orderId=orderIdJson.get("order_id").toString();
                    test2(orderId,proxy);
                }
            }

        }catch (Exception e){
            System.out.println("#####创建订单成功####"+result);
        }
    }
    public void test2(String orderId,Proxy proxy){
        payUrl=payUrl+orderId;
        String result = HttpRequest.post(payUrl).setProxy(proxy)
                .header("token",token).execute().body();
        System.out.println("####支付结果######"+result);
    }
}
